function dvf_folder = create_dvfs_slices_toolbox(patient)
    % Create DVF Slices;----------------------------------------
    %|  Checks if DVF slices exist;
    %|  If not, slices the DVFs as created by Elastix (Transformix) into coronal slices;
    %|
    %|  Creates a single .mat file for each coronal slice, which contains;
    %|      4-D double structure {scans, jj,kk,3}
    %|      i.e. dvfs from each scan for a coronal slice, in 3 (u,v,w) directions;
    %|      dvf_tmp(2,:,:,1) = DVF from scan 2 to ref in the u direction;
    %|      dvf_tmp(2,:,:,2) = DVF from scan 2 to ref in the v direction;
    %|      dvf_tmp(2,:,:,3) = DVF from scan 2 to ref in the w direction;
    %------------------------------------------------------------------------
    %|      Dependancies;                                                     |
    %|             savefast.m                                                 |
    %------------------------------------------------------------------------
    %   This file is part of the
    %   5D-Novel4DCT Toolbox  ("Novel4DCT-Toolbox")
    %   DH Thomas, Ph.D
    %   University of California, Los Angeles
    %   Contact: mailto:dhthomas@mednet.ucla.edu
    %------------------------------------------------------------------------
    % $Author: DHThomas $	$Date: 2014/04/01 10:23:59 $	$Revision: 0.1 $
    
    dvf_folder=[patient.folder sprintf('/DVF_slices_ref%d',patient.ref)];
    mkdir(dvf_folder);
    
    %% Check if DVF Slices have already been created, if not create them;
    dvf_dir = dir(dvf_folder);
    
    % If patient.dvf_folder has less tham dim(1) files in it, DVFs need to be sliced;
    if size(dvf_dir,1)-2<patient.dim(1);
        
           
        %Find folder containing first set of Elastix Registrations;
        folder_dvf1=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,1)];
        fileinfo = dir([folder_dvf1 '/deformationField.raw'])
        %check size of DVF, to ensure that all DVFs will fit in memory;
        fileSize = fileinfo.bytes/1E6
        
        % Using a machine with 16GB of RAM, the following rules should
        % apply;    fileSize<400MB - all 25 scans will fit in memory;
        %           fileSize>400MB <800MB - up 13 scans will fit in memory;
        %           fileSize>800MB  - upto 8 scans will fit in memory;
        %The following loops split the data accordingly 
        %(*rewrite with better memory checks*);
        
        
        if fileSize<400;
            display('FileSize<400; therefore using 1 loop to slice DVFs')
            for i=1:patient.scans;
                
                sprintf('Loading DVF; scan = %d of %d',i, patient.scans)
                
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                
                
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(:,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(:,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(:,:,3,:)));
                clear dvf_tmp;
            end
            
            
            % Following loop slices the dvf structure into coronal slices,
            % and saves the data.
            for slice=1:patient.dim(1);
                
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice,:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice,:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice,:,:));
                
                %         display(sprintf('Saving dvfs_slice_%d',slice))
                %             iSaveX_dvf( sprintf('dvfs_slice_%d',slice),dvf_tmp );
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
%                 savefast([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            
            
            
            
        elseif  fileSize>800; % Split 4D DVF into 3 blocks;
            display('FileSize>800MB; therefore using 3 loops to slice DVFs')
            
            slices_1=1:round(patient.dim(1)/3);
            clear dvf
            for i=patient.ref:patient.scans;
                sprintf('Loop #1 of 3; Loading DVF; scan = %d of %d',i, patient.scans)          
               
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,3,:)));
                clear dvf_tmp;
            end
            
            for slice=slices_1;%1:round(dim(1)/2);%dim(1);
                display(slice)
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice,:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice,:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice,:,:));
                
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            
            clear dvf
            %%
            slices_2=round(patient.dim(1)/3)+1:2*(round(patient.dim(1)/3)+1);%dim(1);
            for i=patient.ref:patient.scans;
                
                sprintf('Loop #2 of 3;  Loading DVF; scan = %d of %d',i, patient.scans)
                
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,3,:)));
                clear dvf_tmp;
            end
        
         
            for slice=slices_2;%1:round(dim(1)/2);%dim(1);
                display(slice)
                
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice-round(patient.dim(1)/3),:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice-round(patient.dim(1)/3),:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice-round(patient.dim(1)/3),:,:));
                
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            %%
            clear dvf
            slices_3=2*(round(patient.dim(1)/3)+1)+1:patient.dim(1);
            for i=patient.ref:patient.scans;
                
                sprintf('Loop #3 of 3; Loading DVF; scan = %d of %d',i, patient.scans)
                
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(slices_3,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(slices_3,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(slices_3,:,3,:)));
                clear dvf_tmp;
            end
            
            
            for slice=slices_3;%1:round(dim(1)/2);%dim(1);
                display(slice)
                
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice-2*(round(patient.dim(1)/3)+1),:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice-2*(round(patient.dim(1)/3)+1),:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice-2*(round(patient.dim(1)/3)+1),:,:));
                
                %         display(sprintf('Saving dvfs_slice_%d',slice))
                %         iSaveX_dvf( sprintf('dvfs_slice_%d',slice),dvf_tmp);
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            
        elseif  fileSize>401 && fileSize<799; % Split 4D DVF into 2 blocks;
            display('FileSize>400MB; therefore using 2 loops to slice DVFs')
            
            slices_1=1:round(patient.dim(1)/2);
            clear dvf
            for i=patient.ref:patient.scans;
%                 cd(folder)
                sprintf('Loop #1 of 2; Loading DVF; scan = %d of %d',i, patient.scans)
                
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(slices_1,:,3,:)));
                clear dvf_tmp;
            end
%             cd(folder)
%             dvf_folder=sprintf('DVF_slices_ref%d',patient.ref);
%             mkdir(dvf_folder)
%             cd(dvf_folder)
            for slice=slices_1;%1:round(dim(1)/2);%dim(1);
                display(slice)
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice,:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice,:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice,:,:));
                
                %         display(sprintf('Saving dvfs_slice_%d',slice))
                %         iSaveX_dvf( sprintf('dvfs_slice_%d',slice),dvf_tmp);
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            
            clear dvf
            
            slices_2=round(patient.dim(1)/2)+1:patient.dim(1);
            for i=patient.ref:patient.scans;
                
                sprintf('Loop #2 of 2; Loading DVF; scan = %d of %d',i, patient.scans)
                
                folder_dvf=[patient.folder_elastix sprintf('/out_phase%d_phase%d',patient.ref,i)];
                dvf_tmp=single(metaImageRead([folder_dvf '/deformationField']));
                dvf.u(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,1,:)));
                dvf.v(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,2,:)));
                dvf.w(i,:,:,:)=single(squeeze(dvf_tmp(slices_2,:,3,:)));
                clear dvf_tmp;
            end
            
%             cd(folder)
%             dvf_folder=sprintf('DVF_slices_ref%d',patient.ref);
%             mkdir(dvf_folder)
%             cd(dvf_folder)
            
            for slice=slices_2;%1:round(dim(1)/2);%dim(1);
                display(slice)
                
                dvf_tmp=zeros(patient.scans, patient.dim(2), patient.dim(3),3);
                
                dvf_tmp(:,:,:,1)=squeeze(dvf.u(:,slice-round(patient.dim(1)/2),:,:));
                dvf_tmp(:,:,:,2)=squeeze(dvf.v(:,slice-round(patient.dim(1)/2),:,:));
                dvf_tmp(:,:,:,3)=squeeze(dvf.w(:,slice-round(patient.dim(1)/2),:,:));
                
                %         display(sprintf('Saving dvfs_slice_%d',slice))
                %         iSaveX_dvf( sprintf('dvfs_slice_%d',slice),dvf_tmp);
                save([dvf_folder sprintf('/dvfs_slice_%d',slice)],'dvf_tmp');
                
            end
            
        end
        
        
    else
        display('DVF Slices already exist...')
    end
    
end

